﻿using NPOI.HSSF.UserModel;
using OMG.Fill.Vegetable.Model.Ent;
using OMG.Fill.Vegetable.Service;
using ProjectShare;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Web;
using System.Web.Http;
namespace OMG.Fill.Vegetable.Model.UI.Controllers
{
    public class VegTecPromotionController : ApiController
    {
        VegTecPromotionServices vegTecPromotionServices = new VegTecPromotionServices();
        //[ApiActionAuthorize("获取蔬菜技术推广人员情况表T_VegTecPromotion分页列表对象操作")]
        //[ApiActionException]
        public Pager<List<VegTecPromotionEntity>> PostPageList(VegTecPromotionQuery query)
        {
            return vegTecPromotionServices.GetPageList(query);
        }
        //[ApiActionAuthorize("新增蔬菜技术推广人员情况表T_VegTecPromotion指定对象操作")]
        //[ApiActionException]
        public ResResult<int> Post(VegTecPromotionEntity model) {
            return vegTecPromotionServices.AddInfo(model);
        }
        //[ApiActionAuthorize("修改蔬菜技术推广人员情况表T_VegTecPromotion指定对象操作")]
        //[ApiActionException]
        public ResResult<int> Put(VegTecPromotionEntity model)
        {
            return vegTecPromotionServices.UpdateInfo(model);
        }
        //[ApiActionAuthorize("删除蔬菜技术推广人员情况表T_VegTecPromotion指定对象操作")]
        //[ApiActionException]
        public ResResult<int> Delete(string id)
        {
            return vegTecPromotionServices.DeleteInfo(id);
        }
        //[ApiActionAuthorize("获取蔬菜技术推广人员情况表T_VegTecPromotion指定对象操作")]
        //[ApiActionException]
        public ResResult<VegTecPromotionEntity> Get(string id)
        {
            return vegTecPromotionServices.GetModel(id);
        }
        /// <summary>
        /// 获取本年和上一年的数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ResResult<VegTecPromotionViewModel> PostData(VegNowModel model)
        {
            return vegTecPromotionServices.GetModel(model);
        }



        /// <summary>
        /// 导出下载响应
        /// </summary>
        /// <param name="ReportCode"></param>
        public void GetExcelData(string ReportCode, string FillInYear, string AreaCode)
        {
            VegNowModel model = new VegNowModel();
            model.AreaCode = AreaCode;
            model.FillInYear = FillInYear;
            model.ReportCode = ReportCode;
            //打开完成excel文件并下载
            HttpContext curContext = HttpContext.Current;
            // 设置编码和附件格式
            curContext.Response.AddHeader("Access-Control-Allow-Origin", "*");
            curContext.Response.ContentType = "application/vnd.ms-excel";
            curContext.Response.ContentEncoding = Encoding.UTF8;
            curContext.Response.Charset = "";
            string strFileName = model.FillInYear + "蔬菜机构人员情况.xls";
            curContext.Response.AppendHeader("Content-Disposition",
                "attachment;filename=" + HttpUtility.UrlEncode(strFileName, Encoding.UTF8));
            curContext.Response.BinaryWrite(vegTecPromotionServices.Method(model).GetBuffer());
            curContext.Response.End();
        }
        public ResResult<List<VegTecPromotionStatsModel>> PostStats(VegNowModel model)
        {
            return vegTecPromotionServices.GetStatsList(model);
        }
        public ResResult<List<VegTecPromotionStatsModel>> PostDetailStats(VegNowModel model)
        {
            return vegTecPromotionServices.GetDetailStats(model);
        }
        public void GetStatsExcelData(string AreaCode, string FillInYear, string Status)
        {
            VegNowModel model = new VegNowModel();
            model.AreaCode = AreaCode;
            model.FillInYear = FillInYear;
            model.Status = Status;
            //打开完成excel文件并下载
            HttpContext curContext = HttpContext.Current;
            // 设置编码和附件格式
            curContext.Response.AddHeader("Access-Control-Allow-Origin", "*");
            curContext.Response.ContentType = "application/vnd.ms-excel";
            curContext.Response.ContentEncoding = Encoding.UTF8;
            curContext.Response.Charset = "";
            string strFileName = model.FillInYear + "蔬菜技术推广机构及人员情况统计表(全面统计).xls";
            curContext.Response.AppendHeader("Content-Disposition",
                "attachment;filename=" + HttpUtility.UrlEncode(strFileName, Encoding.UTF8));
            curContext.Response.BinaryWrite(vegTecPromotionServices.MethodStats(model).GetBuffer());
            curContext.Response.End();
        }
        
    }
}